const dbpool = require('../config/dbpoolconfig');

const authorityController = {
    // 管理员列表
    adminList(req, res) {
        dbpool.connect('select * from adminauthority', [], (err, data) => {
            if (!err) {
                res.send(data);
            }
        })
    },
    // 搜索
    adminSearch(req, res) {
        let adminId = req.query.adminId || '';
        let name = req.query.name || '';
        let tel = req.query.tel || '';
        let principal = req.query.principal || '';
        let role = req.query.role || '';
        let mode = req.query.mode || '';
        let loginDate = req.query.loginDate || '';
        let addDate = req.query.addDate || '';


        let sql = 'select * from adminauthority where 1=1 ';
        let params = [];

        if (adminId.trim() != '') {
            sql += 'and adminId like ? ';
            adminId = '%' + adminId.trim() + '%';
            params.push(adminId)
        }
        if (name.trim() != '') {
            sql += 'and name like ?';
            name = '%' + name.trim() + '%';
            params.push(name)
        }

        if (tel.trim() != '') {
            sql += 'and tel like ?';
            tel = '%' + tel.trim() + '%';
            params.push(tel)
        }
        if (principal.trim() != '') {
            sql += 'and principal like ?';
            principal = '%' + principal.trim() + '%';
            params.push(principal)
        }

        if (role != '' && role != '全部') {
            sql += 'and role = ? ';
            params.push(role)
        }
        if (mode != '' && mode != '全部') {
            sql += 'and mode = ? ';
            params.push(mode)
        }
        if (loginDate != '') {
            sql += 'and loginDate between ? and ? ';
            params.push(loginDate[0], loginDate[1])
        }
        if (addDate != '') {
            sql += 'and addDate between ? and ? ';
            params.push(addDate[0], addDate[1])
        }

        dbpool.connect(sql, params, (err, data) => {
            if (!err) {
                res.send(data);
            }
        })

    },
    // 添加
    addAdmin(req, res) {
        let addAdminData = req.body.addAdminData || req.query.addAdminData;
        // 获取当前日期
        let date = new Date();
        let year = date.getFullYear();
        let month = (date.getMonth() + 1).toString().padStart(2, 0);
        let day = date.getDate().toString().padStart(2, 0);
        let hour = date.getHours().toString().padStart(2, 0);
        let minute = date.getMinutes();

        let time = year + '-' + month + '-' + day + ' ' + hour + ':' + minute


        if (addAdminData['mode'] == '2') {
            addAdminData['mode'] = '正常'
            addAdminData['operate'] = '冻结'
        } else {
            addAdminData['mode'] = '已冻结'
            addAdminData['operate'] = '解冻'
        }
        addAdminData['loginDate'] = '' + time + ''
        addAdminData['addDate'] = '' + time + ''
        addAdminData['superior'] = 'supper'

        let params = [null]
        for (let key in addAdminData) {
            params.push('' + addAdminData[key] + '')
        }

        dbpool.connect('insert into adminauthority values (?,?,?,?,?,?,?,?,?,?,?)',
            params,
            (err, data) => {
                if (!err) {
                    res.send('success')
                }
            })
    },
    // 批量删除
    delSelectionAdmin(req, res) {
        let delSelection = req.query.delSelectionData || '';
        let sql = 'delete from adminauthority where adminId = ? ';
        let params = [];
        if (delSelection != '' || delSelection != []) {
            if (delSelection.length > 1) {
                for (let i = 1; i < delSelection.length; i++) {
                    sql += 'or adminId = ? '
                }
                delSelection.forEach(item => {
                    params.push(item)
                })
            } else {
                delSelection.forEach(item => {
                    params.push(item)
                })
            }
            console.log(delSelection);
            dbpool.connect(sql, params, (err, data) => {
                if (!err) {
                    res.send('success')
                }
            })
        } else {
            res.send('null')
        }
    },
    // 修改状态
    alterAdminMode(req, res) {
        let adminId = req.query.adminId || req.body.adminId;
        let mode = req.query.mode || req.body.mode;
        let operate = req.query.operate || req.body.operate;

        let params = []


        if (mode == '正常') {
            mode = '已冻结'
            operate = '解冻'
            params.push(mode, operate, adminId)
        } else {
            mode = '正常'
            operate = '冻结'
            params.push(mode, operate, adminId)
        }

        dbpool.connect('UPDATE adminauthority SET mode = ?,operate=? WHERE adminId = ?', params, (err, data) => {
            if (!err) {

                res.send('success')
            }
        })
    },
    // 修改信息
    alterAdminData(req, res) {
        let updateData = req.query.updateAdminData || req.body.updateAdminData;
        if (updateData.mode == 2) {
            updateData.mode = '正常',
                updateData.operate = '冻结'
        } else {
            updateData.mode = '已冻结',
                updateData.operate = '解冻'
        }
        if (updateData.role == '超级管理员') {
            updateData.superior = ''
        } else if (updateData.role == '管理员') {
            updateData.superior = 'supper'
        }

        let sql = 'UPDATE adminauthority SET role=?, img = ?,name=?,tel=?,principal=?,superior = ?,mode=?,operate=? WHERE adminId = ?'
        let params = [updateData.role, updateData.img, updateData.name, updateData.tel, updateData.principal, updateData.superior, updateData.mode, updateData.operate, updateData.adminId]

        dbpool.connect(sql, params, (err, data) => {
            if (!err) {
                res.send('success')
            }
        })
    },
    // 删除该用户
    delAdmin(req, res) {
        let adminId = req.query.adminId || req.body.adminId;
        dbpool.connect('delete from adminauthority where adminId = ?', [adminId], (err, data) => {
            if (!err) {
                res.send('success')
            }
        })
    },

    // 用户管理
    userAuthorityList(req, res) {
        dbpool.connect('select * from userauthority', [], (err, data) => {
            if (!err) {
                res.send(data);
            }
        })
    },
    // 新增
    addUserAuthorityList(req, res) {
        let addUserTypeData = req.body.addUserTypeData || req.query.addUserTypeData;
        // 获取当前日期
        let date = new Date();
        let year = date.getFullYear();
        let month = (date.getMonth() + 1).toString().padStart(2, 0);
        let day = date.getDate().toString().padStart(2, 0);
        let hour = date.getHours().toString().padStart(2, 0);
        let minute = date.getMinutes();

        let time = year + '-' + month + '-' + day + ' ' + hour + ':' + minute
        addUserTypeData['createDate'] = '' + time + ''
        let params = [null]

        if(addUserTypeData.des == '超级管理员权限'){
            addUserTypeData.authority = '用户管理,群组管理,直播管理,钱包管理,权限管理'
        }else if(addUserTypeData.des == '管理员权限'){
            addUserTypeData.authority = '用户管理,群组管理,直播管理,钱包管理'
        }else{
            addUserTypeData.authority = ''
        }
        for (let key in addUserTypeData) {
            params.push('' + addUserTypeData[key] + '')
        }

        dbpool.connect('insert into userauthority values (?,?,?,?,?)',
            params,
            (err, data) => {
                if (!err) {
                    res.send('success')
                }
            })
    },
    // 批量删除
    delSelectionUserAuthority(req,res){
        let delSelection = req.query.delSelectionData || '';
        let sql = 'delete from userauthority where userId = ? ';
        let params = [];
        if (delSelection != '' || delSelection != []) {
            if (delSelection.length > 1) {
                for (let i = 1; i < delSelection.length; i++) {
                    sql += 'or userId = ? '
                }
                delSelection.forEach(item => {
                    params.push(item)
                })
            } else {
                delSelection.forEach(item => {
                    params.push(item)
                })
            }
            dbpool.connect(sql, params, (err, data) => {
                if (!err) {
                    res.send('success')
                }
            })
        } else {
            res.send('null')
        }
    },
    // 修改
    updateUserAuthority(req,res){
        let updateData = req.query.updateUserTypeData || req.body.updateUserTypeData;

        if(updateData.des == '超级管理员权限'){
            updateData.authority = '用户管理,群组管理,直播管理,钱包管理,权限管理'
        }else if(updateData.des == '管理员权限'){
            updateData.authority = '用户管理,群组管理,直播管理,钱包管理'
        }else{
            updateData.authority = ''
        }
        let sql = 'UPDATE userauthority SET name = ?,des=?,authority = ? WHERE userId = ?'
        let params = [updateData.name, updateData.des,updateData.authority, updateData.userId]

        dbpool.connect(sql, params, (err, data) => {
            if (!err) {
                res.send('success')
            }
        })
    },
    // 权限
    updateAuthority(req,res){
        let userId = req.query.updateAuthorityId || req.body.updateAuthorityId
        let authority = req.query.updateAuthority || ''

        if(authority != '' || authority != []){
            authority = ''+authority.toString()+''
        }
        
        dbpool.connect('update userauthority set authority = ? where userId = ?',[authority,userId],(err,data)=>{
            console.log(err);
            console.log(data);
            if(!err){
                res.send('success')
            }
        })
    },
    // 删除
    delUserAuthority(req,res){
        let userId = req.query.delUserId || req.body.delUserId;

        dbpool.connect('delete from userauthority where userId = ?', [userId], (err, data) => {
            if (!err) {
                res.send('success')
            }
        })
    }
}

module.exports = authorityController;